$(function () {
  if ('ontouchstart' in document) {
    $('body').addClass('hasTouch');
  }

  $.setupHeader();
  $.setupHome();
  $.setupEvents();

  $('ul:first', '#stories').after('<ul class="left"></ul><ul class="right"></ul>').addClass('default').find('li').each(function() {
    var clone = $(this).clone().removeClass('odd');
    if ($(this).hasClass('odd')) {
      clone.appendTo('#stories ul.left');
    } else {
      clone.appendTo('#stories ul.right');
    }
  });

  $('.wp-caption img[alt]', '#story').each(function () {
    var image = $(this),
        alt = image.attr('alt');

    if ($.trim(alt) !== '') {
      image.after('<p class="wp-caption-desc">' + alt + '</p>');
    }
  });

  if ($('#story')[0]) {
    var lastStoryNode = $('.content>p, .content>ul, .content>ol', '#story').filter(':last');
    if (lastStoryNode.is('p')) {
      lastStoryNode.append(' &#10022;');
    } else {
      lastStoryNode.find('li:last').append(' &#10022;');
    }
  }

  $('.top a').on('click', function () {
    $.scrollTo(0, 800);
    return false;
  });
});

$.setupHeader = function () {
  var _body = $('body'),
      dropdownToggle = $('.dropdown>span', '#nav-primary'),
      search = $('#search'),
      searchToggle = $('#goto-search');

  var dropdownMobile = $('.dropdown>div', '#nav-primary').clone().insertAfter('#branding').attr('id', 'dropdown-handheld');

  // mobile support
  dropdownToggle.on('click', function () {
    dropdownToggle.parent().toggleClass('active');
    dropdownMobile.toggleClass('active');
    return true;
  });
  if (_body.hasClass('hasTouch')) {
    _body.on('touchend', function() {
      if ($('body').width() > 640 && dropdownToggle.parent().hasClass('active')) {
        dropdownToggle.parent().removeClass('active');
      }
    });
    dropdownToggle.parent().on('touchend', function(e) {
      if ($('body').width() > 640) {
        e.stopPropagation();
      }
    });
  }

  // animation is handled by CSS, JS just assigns the class and focuses/blurs the field
  searchToggle.on('click', function () {
    _body.toggleClass('searchActive');
    $(this).toggleClass('active');
    if (_body.is('.searchActive')) {
      search.find('input').focus();
    } else {
      search.find('input').blur();
    }
    return false;
  });

  // search submission disabled if content is empty
  search.on('submit', function () {
    if (search.hasClass('disabled')) {
      return false;
    }
  }).addClass('disabled').find('input').on('keyup blur input', function () {
    if ($.trim($(this).val()) !== '') {
      search.removeClass('disabled');
    } else {
      search.addClass('disabled');
    }
  });

  // if ESC pressed while search is active then hide search
  $(document).keyup(function (e) {
    if (e.keyCode === 27 && _body.is('.searchActive')) {
      searchToggle.trigger('click');
    }
  });
};

$.setupHome = function () {
  var _body = $('body'),
      home = $('#home'),
      featured = $('#home-featured'),
      stories = $('#home-stories');

  if (!featured[0]) {
    return;
  }

  // generate featured images
 /* Isolated by Eric var homeFeaturedImage = $('<img src="' + featured.data('image-color') + '" id="home-featured-image">').prependTo(home).hide().on('load', function () {
    $(this).show();
    $(window).trigger('resize.homeFeaturedImage');
  });
  var homeFeaturedImageBW = $('<img src="' + featured.data('image-bw') + '" id="home-featured-image-bw">').prependTo(home).hide().on('load', function () {
    $(this).show();
    $(window).trigger('resize.homeFeaturedImage');
  });
*/
  // create resize events for the featured images
  $(window).on('resize.homeFeaturedImage', function () {
    var height = home.height(),
        width = home.width(),
        imgHeight = homeFeaturedImage.height(),
        imgWidth = homeFeaturedImage.width();

    // allows to operate under the assumption that search is NOT expanded
    if (_body.is('.searchActive')) {
      height += $('#search').height();
    }

    // either span the full height or the full width
    if (height / width > imgHeight / imgWidth) {
      homeFeaturedImage.add(homeFeaturedImageBW).css({
        height: height + 'px',
        width: 'auto',
        marginTop: 0
      });

      // center image horizontally
      imgWidth = homeFeaturedImage.width();
      if (imgWidth > width) {
        homeFeaturedImage.add(homeFeaturedImageBW).css('margin-left', ((width - imgWidth) / 2) + 'px');
      }
    } else {
      homeFeaturedImage.add(homeFeaturedImageBW).css({
        height: 'auto',
        width: width + 'px',
        marginLeft: 0 // reset image centering
      });

      // center image horizontally
      imgHeight = homeFeaturedImage.height();
      if (imgHeight > height) {
        homeFeaturedImage.add(homeFeaturedImageBW).css('margin-top', ((height - imgHeight) / 2) + 'px');
      }
    }
  }).trigger('resize.homeFeaturedImage');

  // mouse over for story links
  if (!_body.hasClass('hasTouch')) {
    stories.find('li a').on('mouseenter', function () {
      _body.addClass('storyActive');
    }).on('mouseleave', function () {
      _body.removeClass('storyActive');
    });
  }
};

$.setupEvents = function () {
  var navLocal = $('nav a', '#events-calendar');

  navLocal.on('click', function () {
    var url = $(this).attr('href').split('#'),
        currentURL = window.location.pathname;

    if (url[0].indexOf(currentURL, url[0].length - currentURL.length) === -1) {
      return true;
    }

    $.scrollTo('#' + url[1], 800);

    return false;
  });
};